﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace WebLog
{
    /// <summary>
    /// 查询dto
    /// </summary>
    public class SearchLogInputDto
    {

        [Description("开始时间")]
        public DateTime? BeginTime { get; set; }
        [Description("结束时间")]
        public DateTime? EndTime { get; set; }
        [Description("路径")]
        public string Path { get; set; }
        [Description("ContentType")]
        public string ContentType { get; set; }
        [Description("Headers")]
        public string Headers { get; set; }
        [Description("Query")]
        [IsShow(true)]
        public string Query { get; set; }
        [Description("Body")]
        [IsShow(true)]
        public string Body { get; set; }
        [Description("Data")]
        [IsShow(true)]
        public string Data { get; set; }
        [Description("Message")]
        [IsShow(true)]
        public string Message { get; set; }
        [Description("第几行起")]
        [IsShow(false)]
        public int Skip { get; set; }
        [Description("第几行结束")]
        [IsShow(false)]
        public int Take { get; set; }
        public int CurrentPageIndex { get; set; }
        public int PageSize { get; set; }
        [Description("操作人")]
        public string RealName { get; set; }
        [Description("排序字段")]
        public OrderFields? OrderField { get; set; }
        [Description("是否降序")]
        [DefaultValue(true)]
        public bool IsDesc { get; set; } = true;
        [Description("成功与否")]
        public bool? Success { get; set; } = null;
        [Description("请求类型")]
        public string Method { get; set; }
        [Description("耗时>")]
        public decimal? CompleteBegin { get; set; }
        [Description("耗时<")]
        public decimal? CompleteEnd { get; set; }
        [Description("请求id")]
        [IsShow(false)]
        public int? ID { get; set; }
        [Description("备注(标记查询用的)")]
        public string Memo { get; set; }
        //是否记录 默认查询都记录 在历史跳转的时候则不记录
        [IsShow(false)]
        public bool IsNotRecord { get; set; }
        /// <summary>
        /// 分页开关
        /// </summary>
        [IsShow(false)]
        public bool PageSkipClose { get; set; }
        /// <summary>
        /// 状态验证（后端调用的时候可以忽视状态进行查询）
        /// </summary>
        [IsShow(false)]
        internal bool VerifyStatus { get; set; } = true;
        [IsShow(false)]
        public BigNode Node { get; set; }
        /// <summary>
        /// 所有的组成的sql语句
        /// </summary>
        [IsShow(false)]
        public string AllSql { get; set; }
        internal Guid wlogid;
    }
    public enum OrderFields
    {
        Date,
        Success,
        Path,
        Message,
        Body,
        Query,
        Method,
        IsHttps,
        Data,
        Id,
    }
    public enum NodeType
    {
        None = 0,
        Begin = 1,//左括号
        End = 2,//右括号
        Single = 3,//单单一个条件
    }
    public class Node
    {
        public NodeType NodeType { get; set; }
        public string Filed { get; set; }
        public string Symbol { get; set; }
        public string Val { get; set; }
        public bool Not { get; set; }
        private string _sql;
        public string Sql
        {
            get
            {
                if (Filed.IsNullEmpty()) return "";
                else if (!_sql.IsNullEmpty()) return _sql;
                var val = Val;
                if (Symbol?.ToLower() == "in")//做些防止注入的限制
                {
                    val = $"({string.Join(',', val.Split(',').Select(s => s[0] == '\'' ? s : $"'{s}'"))})";
                }
                else val = $"'{val}'";
                _sql = string.Format("{3}{5}{0} {1} {2} {4}", Filed, Symbol, val
                    , NodeType == NodeType.Begin || NodeType == NodeType.Single ? "(" : ""
                    , NodeType == NodeType.End || NodeType == NodeType.Single ? ")" : ""
                    , Not ? "not " : "");
                return _sql;
            }
        }
    }
    public class BigNode : Node//一个bigNode 就是一个 括号，然后接入or？ 现在在于不纯粹的链条这个类型是；需要处理下
    {
        public BigNode Prev { get; set; }
        public BigNode Next { get; set; }
        /// <summary>
        /// 连接的逻辑符
        /// </summary>
        public string UnionLogicalSymbol { get; set; }
        public string GetUnions()
        {
            StringBuilder sb = new StringBuilder();
            GetUnions(sb);
            return sb.ToString();
        }
        private void GetUnions(StringBuilder sb)
        {
            sb = sb ?? new StringBuilder();
            if (!Sql.IsNullEmpty())
            {
                sb.Append(Sql);
                if (!UnionLogicalSymbol.IsNullEmpty())
                {
                    sb.Append(' ');
                    sb.Append(UnionLogicalSymbol);
                    sb.Append(' ');
                }
            }
            if (Next != null)
                Next.GetUnions(sb);
        }
    }
}